package vn.njai.thousand_english_word.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.gc.materialdesign.views.CheckBox;
import vn.njai.thousand_english_word.Entities.NodeItem;
import vn.njai.thousand_english_word.R;
import vn.njai.thousand_english_word.Utils.DataManager;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by VUONG on 12/14/2014.
 */
public class CheckActivity extends ActionBarActivity {
    private String TAG = "CheckActivity";
    private Context mContext;
    private NodeItem mQuestion;
    private int mIndexCorrect;
    private int mIndexQuestion;
    private ArrayList<CheckBox> mCheckBoxList;
    private ArrayList<TextView> mTextViewList;
    private ArrayList<NodeItem> mWrongAnswerList;

    @InjectView(R.id.toolbar) public Toolbar mToolbar;
    @InjectView(R.id.checkBox1) public CheckBox mCheckBox1;
    @InjectView(R.id.checkBox2) public CheckBox mCheckBox2;
    @InjectView(R.id.checkBox3) public CheckBox mCheckBox3;
    @InjectView(R.id.checkBox4) public CheckBox mCheckBox4;
    @InjectView(R.id.btnNext) public ImageButton mNextButton;
    @InjectView(R.id.txtQuestion) public TextView mQuestionText;
    @InjectView(R.id.txtAnswer1) public TextView mAnswer1Text;
    @InjectView(R.id.txtAnswer2) public TextView mAnswer2Text;
    @InjectView(R.id.txtAnswer3) public TextView mAnswer3Text;
    @InjectView(R.id.txtAnswer4) public TextView mAnswer4Text;


    public static void start(Context context) {
        Intent intent = new Intent(context,CheckActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check);
        mContext = getApplicationContext();
        init();
    }

    public void init(){
        ButterKnife.inject(this);
        initActionBar();
        initListItems();
        initQuestion();
    }

    public void initQuestion(){
        if(DataManager.getInstance().mNeedLearn.mNeedLearnList.size()>1) {
            Random random = new Random();
            mIndexQuestion = random.nextInt(DataManager.getInstance().mNeedLearn.mNeedLearnList.size() - 1) + 1;
            mQuestion = null;
            mQuestion = DataManager.getInstance().getNodeAt(DataManager.getInstance().mNeedLearn.mNeedLearnList.get(mIndexQuestion));
            mIndexCorrect = random.nextInt(4);
            getWrongAnswer();
        }
    }

    public void initListItems(){
        mTextViewList = new ArrayList<TextView>();
        mTextViewList.add(mAnswer1Text);
        mTextViewList.add(mAnswer2Text);
        mTextViewList.add(mAnswer3Text);
        mTextViewList.add(mAnswer4Text);

        mCheckBoxList = new ArrayList<CheckBox>();
        mCheckBoxList.add(mCheckBox1);
        mCheckBoxList.add(mCheckBox2);
        mCheckBoxList.add(mCheckBox3);
        mCheckBoxList.add(mCheckBox4);
    }

    public void initView(){
        for (int i = 0;i< mCheckBoxList.size();i++){
            TextView textView = mTextViewList.get(i);
            textView.setText("");
            CheckBox checkBox = mCheckBoxList.get(i);
            checkBox.setEnabled(true);
            checkBox.setChecked(false);
            YoYo.with(Techniques.SlideInLeft)
                    .duration(700)
                    .playOn(checkBox);
        }

        mQuestionText.setText(getString(R.string.question_body_1) + "\"" + mQuestion.mWord + "\"" + getString(R.string.question_body_2));
        YoYo.with(Techniques.SlideInLeft)
                .duration(700)
                .playOn(mQuestionText);
        mTextViewList.get(mIndexCorrect).setText(mQuestion.mMeans);
        int index = 0;
        for(int i = 0;i<mTextViewList.size();i++){
            if(i!=mIndexCorrect){
                mTextViewList.get(i).setText(mWrongAnswerList.get(index).mMeans);
                index++;
            }
        }

        for (int i = 0;i< mCheckBoxList.size();i++){
            final int finalI = i;
            mCheckBoxList.get(i).setOncheckListener(new CheckBox.OnCheckListener() {
                @Override
                public void onCheck(boolean isChecked) {
                    for(int j = 0;j < mCheckBoxList.size();j++){
                        mCheckBoxList.get(j).setEnabled(false);
                    }
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            test(finalI);
                        }
                    },500);
                }
            });
        }
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                next();
            }
        });

        for (int i = 0;i< mTextViewList.size();i++){
            TextView textView = mTextViewList.get(i);
            YoYo.with(Techniques.SlideInLeft)
                    .duration(700)
                    .playOn(textView);
        }
    }

    public void getWrongAnswer(){
        mWrongAnswerList = new ArrayList<NodeItem>();
        ArrayList<Integer> mWrongIndexList = new ArrayList<Integer>();
        while (mWrongIndexList.size()<3){
            Random random = new Random();
            int index = random.nextInt(DataManager.getInstance().mWordCount-1)+1;
            if(index != mQuestion.mRank){
                boolean test = false;
                for(int i = 0;i<mWrongIndexList.size();i++){
                    if(mWrongIndexList.get(i) == index)
                        test = true;
                }
                if(test == false) {
                    mWrongIndexList.add(index);
                    mWrongAnswerList.add(DataManager.getInstance().getNodeAt(index));
                }
            }
        }
        initView();
    }

    public void next(){
        initQuestion();
    }

    public void test(int index){
        YoYo.with(Techniques.Tada)
                .duration(700)
                .playOn(mCheckBoxList.get(mIndexCorrect));
        YoYo.with(Techniques.Tada)
                .duration(700)
                .playOn(mTextViewList.get(mIndexCorrect));
        if(index == mIndexCorrect){
            mQuestion.mRemember = 3;
            DataManager.getInstance().setWordAt(mQuestion);
            DataManager.getInstance().mNeedLearn.mNeedLearnList.remove(mIndexQuestion);
            DataManager.getInstance().saveNeedLearnList();
        }else{
            mCheckBoxList.get(index).setChecked(false);
            mCheckBoxList.get(mIndexCorrect).setChecked(true);
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                next();
            }
        },500);

    }

    private void initActionBar(){
        mToolbar.setTitle(getString(R.string.actionbar_title_check));
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_back);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
